/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui.actions;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import sk.yw.azetclient.Main;
import sk.yw.azetclient.gui.ConversationListFrame;
import sk.yw.azetclient.model.Buddy;
import sk.yw.azetclient.model.Message;
import sk.yw.azetclient.model.MessageThread;

/**
 *
 * @author error216
 */
public class OpenConversationAction extends AbstractAction {
    
    private static final long serialVersionUID = 1722296089L;
    
    public static final Comparator<MessageThread> newestFirstComparator =
                new Comparator<MessageThread>(){
        
        public int compare(MessageThread thread1, MessageThread thread2) {
            if (thread1 == null) return 1;
            if (thread2 == null) return -1;
            
            Message message1 = thread1.getLastMessage();
            Message message2 = thread2.getLastMessage();

            if (message1 == null) return 1;
            if (message2 == null) return -1;
            if (message1.getSendTime() == null) return 1;
            if (message2.getSendTime() == null) return -1;

            return - message1.getSendTime().compareTo(message2.getSendTime());
        }
    };
    
    private Buddy user;
    private Buddy buddy;
    private Component component;
    private ConversationListFrame conversationListFrame;
    
    public OpenConversationAction(Buddy user, Buddy buddy,
            Component component) {
        super(Main.getText("action.openConversation"));
        if (user == null)
            throw new IllegalArgumentException("Null pointer in user");
        
        this.user = user;
        this.buddy = buddy;
        
        putValue(AbstractAction.ACCELERATOR_KEY,
                KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
    }

    public void actionPerformed(ActionEvent e) {
        conversationListFrame = new ConversationListFrame();
        conversationListFrame.setLocationRelativeTo(component);
        conversationListFrame.setVisible(true);
        
        new Thread(){
            @Override
            public void run() {
                final List<MessageThread> threads =
                        Main.getMainFrame().getThreads(buddy);
                Collections.sort(threads, newestFirstComparator);
                SwingUtilities.invokeLater(new Runnable(){
                    public void run() {
                        conversationListFrame.setConversations(user, threads);
                    }
                });
            }
        }.start();
    }

}
